package com.domino.server.login;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.domino.server.user.FacebookApplication;
import com.domino.server.user.FacebookAuthenticateUtil;
import com.domino.server.user.FacebookGraphImpl;
import com.domino.server.user.FacebookUser;


public class LoginServlet extends HttpServlet {

	private FacebookAuthenticateUtil authUtil;
	Logger LOG = Logger.getLogger(getClass().getCanonicalName());
	
	public LoginServlet() {;
		authUtil = new FacebookAuthenticateUtil(new FacebookGraphImpl(FacebookApplication.APP_ID, FacebookApplication.SECRET));
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException, IOException
	{
		doGet(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		if (req.getParameter("error_reason")!=null){
			resp.sendRedirect("/redirect.html");
			return;
		}

		FacebookUser user = authUtil.getUser(req);
		
		/*if (user!=null) { // player has logged in, redirect to real service
			resp.sendRedirect("/redirect.html");
		}*/
		
		resp.getWriter().print(getHeaderStart());
		
		if (user!=null) { // player has logged in, send to real service 
			resp.getWriter().print("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=/\">");
			LOG.info("Redirect user found");
		} 
		
		String color = getColor(user, req.getParameter("code"));
		
		resp.getWriter().print(getHeaderEndAndBodyStart(color));
		if (user!=null) { // player has logged in and user has been found, send to real service
			resp.getWriter().print("<h1>Redirecing to game</h1><h4><a href=\"/\">take me directly game please!</a></h4><br>");
		} else if (req.getParameter("code")!=null) { // player has logged in but cookie is not set
			LOG.info("Redirect code found");
			resp.getWriter().print("<h1>Redirecing to game</h1><h4><a href=\"/redirect.html\">take me directly game!</a></h4><br>");
			//resp.getWriter().print("<img src=\"http://www.segersten.eu/othello/othelloredirect.jpg\"></img>");
			resp.getWriter().print(authUtil.getRedirectJavaScript("/redirect.html"));
		} else {
			LOG.info("No redirect, intall / login to app");
			resp.getWriter().print("<br><center>"+
				"<h1><a href=\"" + authUtil.getInstallApplicationUrl()+ "\" target=\"_top\">");
			resp.getWriter().print("<img align=\"center\" src=\"http://www.segersten.eu/othello/othelloinstall.jpg\"></img>");
			resp.getWriter().print("<br>Click here to play Othello!</a></h1></center>");
			resp.getWriter().print("<br><a href=\"http://www.facebook.com/apps/application.php?id=145065125539157\" target=\"_top\">[Application Information]</a>");
		}
		
		/*resp.getWriter().print("<br><br><hr>User: " + user);
		resp.getWriter().print("<hr>Request params: " + DebugOutputUtil.toString(req.getParameterMap()));
		resp.getWriter().print("<hr>Cookies: " + DebugOutputUtil.toStringArray(req.getCookies()));
		resp.getWriter().print("<hr>Request: " + req);
		resp.getWriter().print("<hr>Request: " + req.getHeader("Referer"));
		*/
		resp.getWriter().print(getBodyEndAndFooter());
		resp.getWriter().flush();
	}
	

	private String getColor(FacebookUser user, String code) {
		return "#FFFFFF";
		/*if (user!=null) {
			return "#EEEEEE";
		}
		if (code!=null) {
			return "#FF0000";
		}
		return "#00FF00";*/
	}

	private String getBodyEndAndFooter() {
		StringBuilder sb = new StringBuilder();
		sb.append("\n</body>\n");
		sb.append("</html>\n");
		return sb.toString();
	}

	private String getHeaderStart() {
		StringBuilder sb = new StringBuilder();
		sb.append("<!doctype html>\n");
		sb.append("<html>\n");
		sb.append("<head>\n");
		sb.append("<link type=\"text/css\" rel=\"stylesheet\" href=\"/Domino.css\">");
		sb.append("<META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-CACHE\">\n");
		sb.append("<META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">\n");
		sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n");
		return sb.toString();
	}
	
	private String getHeaderEndAndBodyStart(String color) {
		StringBuilder sb = new StringBuilder();
		sb.append("</head>\n");
		sb.append("<body bgcolor='"+color+"'>\n");
		return sb.toString();
	}

}
